package com.aaa.tmr.controller;

import com.aaa.tmr.entity.Custom;
import com.aaa.tmr.service.CustomService;
import com.aaa.tmr.util.Result;
import com.aaa.tmr.util.ReturnStatus;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * (Custom)表控制层
 *
 * @author makejava
 * @since 2021-03-19 15:33:36
 */
@Controller
@RequestMapping("custom")
public class CustomController {
    /**
     * 服务对象
     */
    @Resource
    private CustomService customService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Custom selectOne(Integer id) {
        return this.customService.queryById(id);
    }

    /**
     * 跳转客户分页表页面
     * @return
     */
    @RequestMapping("toCustom")
    public String toPage(){
        return "back/customer-page";
    }

    @ResponseBody
    @RequestMapping("queryAll")
    public Result queryAll(Integer page, Integer limit, Custom custom){
        PageInfo pageInfo = customService.queryAll(page, limit, custom);
        return  new Result(ReturnStatus.SUCCESS.getCode(),ReturnStatus.SUCCESS.getMessage(),
                pageInfo.getTotal(), pageInfo.getList());
    }

    @ResponseBody
    @PostMapping("insert")
    public Result  insert(Custom custom){
        return  new Result(ReturnStatus.SUCCESS.getCode(),ReturnStatus.SUCCESS.getMessage(),
                0, customService.insert(custom));
    }

    @ResponseBody
    @PutMapping("update")
    public Result  update(Custom custom){
        return  new Result(ReturnStatus.SUCCESS.getCode(),ReturnStatus.SUCCESS.getMessage(),
                0, customService.update(custom));
    }

    @ResponseBody
    @DeleteMapping("deleteById")
    public Result deleteById(Integer customId) {
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(),0,
                this.customService.deleteById(customId));
    }

    @ResponseBody
    @DeleteMapping("deleteByIds")
    public Result deleteByIds(String customIds) {
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(),0,
                this.customService.batchDeleteById(customIds));
    }

    /**
     * 通过实体作为筛选条件查询
     *
     * @return 对象列表
     */
    @ResponseBody
    @RequestMapping("importCustom")
    public Result importCustom(@RequestParam MultipartFile file) {
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(),
                0,customService.importCustom(file));
    }
}
